package cn.lysj.framework.store.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.validate.annotation.Validator;
import cn.lysj.framework.store.domain.Store;
import cn.lysj.framework.store.service.StoreService;
import cn.lysj.framework.store.vo.EventVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author drj
 * @description 门店控制层
 * @create 2018-10-29 10:02:38
 **/
@Slf4j
@RestController
@Api(description = "门店管理接口")
@RequestMapping("/store")
public class StoreController extends BaseController<Store>{
    @Value("${event.title}")
    public String eventTitle;
    @Value("${event.info}")
    public String eventInfo;
    @Value("${event.img}")
    public String eventImg;

    @Resource
    private StoreService storeService;

    @Override
    public StoreService getService() {
        return storeService;
    }

    @Override
    public Resp save(Store store){
        return null;
    }

    @Override
    public Resp list(Store store, PageVo pageVo){
        return null;
    }

    @PostMapping(value = "/save_rewrite")
    public Resp save(@CurrentUser User user, @Validator Store store){
        return storeService.save(user,store);
    }

    @PostMapping(value = "/upload_rewrite")
    public Resp upload_rewrite(@Validator Store entity){
        Store store = storeService.findOne(entity.getId());
        if(entity.getBusinessType() == 1){
            if(ParamUtil.isBlank(entity.getIsDoBusiness())){
                return new Resp(Resp.PARAM_ERROR,"请选择营业状态");
            }
        }
        //获取Hutool拷贝实例
        CopyOptions copyOptions = CopyOptions.create();
        //忽略为null值得属性
        copyOptions.setIgnoreNullValue(true);
        //进行属性拷贝
        BeanUtil.copyProperties(entity,store,copyOptions);
        storeService.save(store);
        return new Resp("修改成功!");
    }

    @PostMapping(value = "/change_is_do_business")
    public Resp changeIsDoBusiness(String storeId,Boolean isDoBusiness){
        Store store = storeService.findOne(storeId);
        if(ParamUtil.isBlank(store)){
            return new Resp(Resp.PARAM_ERROR,"非法操作!");
        }
        store.setIsDoBusiness(isDoBusiness);
        storeService.save(store);
        return new Resp("修改成功!");
    }


    @GetMapping("/list_rewrite")
    public Resp list(@CurrentUser User user, Store entity, PageVo pageVo){
        entity.setUserId(user.getId());
        Page<Store> stores = storeService.list(entity,pageVo);
        for (Store store:stores){
            if(store.getBusinessType() == 1){
                String time[] = store.getBusinessTime().split("-");
                if(time.length != 2){
                    break;
                }
                store.setIsDoBusiness(storeService.isDoBusiness(time));;
            }
            else{
                break;
            }
        }
        return new Resp(stores);
    }



    @GetMapping("/get_store_config")
    public Resp getAdvanceProvideTime(@CurrentUser User user){
        Store store = storeService.findOne(user.getStoreId());
        return new Resp(store,"获取成功!");
    }

    @PostMapping("/update_integral_ratio")
    public Resp updateIntegralRatio(@CurrentUser User user, BigDecimal integralRatio){
        Store store = storeService.findOne(user.getStoreId());
        if(integralRatio == null){
            return new Resp(Resp.PARAM_ERROR,"请输入积分比例");
        }
        if(integralRatio.compareTo(BigDecimal.ZERO) < 0){
            return new Resp(Resp.PARAM_ERROR,"积分比例只能大等于0!");
        }
        store.setIntegralRatio(integralRatio);
        storeService.save(store);
        return new Resp(store,"修改积分比例成功!");
    }

    @PostMapping("/update_advance_provide_time")
    public Resp updateAdvanceProvideTime(@CurrentUser User user,Integer advanceProvideTime){
        Store store = storeService.findOne(user.getStoreId());
        if(advanceProvideTime == null){
            return new Resp(Resp.PARAM_ERROR,"请输入生日券提前发放天数");
        }
        if(advanceProvideTime < 0){
            return new Resp(Resp.PARAM_ERROR,"生日券提前发放天数只能大等于0!");
        }
        if(advanceProvideTime > 365){
            return new Resp(Resp.PARAM_ERROR,"生日券提前发放天数不能大于365!");
        }
        store.setAdvanceProvideTime(advanceProvideTime);
        storeService.save(store);
        return new Resp(store,"修改生日券提前发放天数成功!");
    }

    @GetMapping("/get_store_config_event_info")
    public Resp getStoreConfigEventInfo(@CurrentUser User user){
        Store store = storeService.findOne(user.getStoreId());
        EventVo eventVo = new EventVo(store.getEventTitle(),store.getEventInfo(),store.getEventImg());
        if(ParamUtil.isBlank(store.getEventTitle())){
            eventVo.setEventTitle(eventTitle);
        }
        if(ParamUtil.isBlank(store.getEventInfo())){
            eventVo.setEventInfo(eventInfo);
        }
        if(ParamUtil.isBlank(store.getEventImg())){
            eventVo.setEventImg(eventImg);
        }
        return new Resp(eventVo);
    }

    @PostMapping("/change_store_config_event_info")
    public Resp changeStoreConfigEventInfo(@CurrentUser User user,String eventTitle,String eventInfo,String eventImg){
        Store store = storeService.findOne(user.getStoreId());
        if(ParamUtil.isBlank(eventTitle)){
            return new Resp(Resp.PARAM_ERROR,"分享活动标题不能为空");
        }
        if(ParamUtil.isBlank(eventInfo)){
            return new Resp(Resp.PARAM_ERROR,"分享活动信息不能为空");
        }
        if(ParamUtil.isBlank(eventImg)){
            return new Resp(Resp.PARAM_ERROR,"分享活动图片不能为空");
        }
        store.setEventInfo(eventInfo);
        store.setEventImg(eventImg);
        store.setEventTitle(eventTitle);
        storeService.save(store);
        return new Resp(store,"设置默认分享活动样式成功!");
    }
}
